package com.neulogistics.contrib.procuredcenter.api.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreatePurchaseOrderCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.PurchaseCommand;
import com.neulogistics.contrib.procuredcenter.api.application.query.IPurchaseOrderQueryService;
import com.neulogistics.contrib.procuredcenter.api.application.query.PurchaseOrderQuery;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.PurchaseOrderViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "采购单管理")
@RequestMapping("/procuredCenter/purchaseOrder")
public class PurchaseOrderController {
    @Resource
    IMediator mediator;

    @Resource
    IPurchaseOrderQueryService purchaseOrderQueryService;

    @ApiOperation("进货")
    @PostMapping("/purchase")
    public BaseResponse<Boolean> purchase(@RequestBody @Validated @NotNull PurchaseCommand command){

        BaseResponse<Boolean> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("新增采购单")
    @PostMapping("/create")
    public BaseResponse<String> create(@RequestBody @Validated @NotNull CreatePurchaseOrderCommand command){

        BaseResponse<String> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id获取采购单")
    @GetMapping("/get/{purchaseOrderId}")
    public BaseResponse<PurchaseOrderViewModel> get(
            @PathVariable(value = "purchaseOrderId") @NotNull @Positive Integer purchaseOrderId){

        PurchaseOrderViewModel purchaseOrderViewModel = purchaseOrderQueryService.getById(purchaseOrderId);

        return ResultUtils.success(purchaseOrderViewModel);
    }

    @ApiOperation("分页查询采购单")
    @PostMapping("/page")
    public BaseResponse<Page<PurchaseOrderViewModel>> page(@RequestBody @Validated @NotNull PurchaseOrderQuery purchaseOrderQuery){

        Page<PurchaseOrderViewModel> viewModelPage = purchaseOrderQueryService.page(purchaseOrderQuery);

        return ResultUtils.success(viewModelPage);
    }
}
